//
//  HMFeedbackController.m
//  HeartMark
//
//  Created by rimi on 16/11/9.
//  Copyright © 2016年 黄华强. All rights reserved.
//

#import "HMFeedbackController.h"
#import <AVOSCloud/AVOSCloud.h>
#import "HMAlertView.h"

@interface HMFeedbackController ()<UITextViewDelegate>

@property (nonatomic, strong) UITextView *adviceTV;
@property (nonatomic, strong) UILabel *placeHolderL;
@property (nonatomic, strong) UIButton *clearButton;

@end

@implementation HMFeedbackController

- (void)viewDidLoad {
    [super viewDidLoad];
    self.view.backgroundColor = [UIColor whiteColor];
    self.automaticallyAdjustsScrollViewInsets = NO;
    [self setupUI];
    self.navigationController.navigationBar.tintColor = [UIColor whiteColor];
}

- (void)setupUI {
    //内容输入框
    _adviceTV = [[UITextView alloc]initWithFrame:CGRectMake(8, 64, SCREEN_WIDTH - 16, SCREEN_HEIGHT - 16 - 64)];
    _adviceTV.showsVerticalScrollIndicator = NO;
    _adviceTV.backgroundColor = [UIColor whiteColor];
    _adviceTV.delegate = self;
    //设置内容位置偏移
    _adviceTV.textContainerInset = UIEdgeInsetsMake(8, 0, 0, 23);
    _adviceTV.font = [UIFont systemFontOfSize:15];
    [self.view addSubview:_adviceTV];
    //plackHolder
    _placeHolderL = [[UILabel alloc]initWithFrame:CGRectMake(8, 2, SCREEN_WIDTH - 20, 30)];
    _placeHolderL.text = @"您的意见能帮助我们做得更好...";
    _placeHolderL.textColor = [UIColor lightGrayColor];
    _placeHolderL.font = [UIFont systemFontOfSize:15];
    _placeHolderL.alpha = 1.0;
    [_adviceTV addSubview:_placeHolderL];
    //clearButton
    _clearButton = [UIButton buttonWithType:UIButtonTypeSystem];
    [_clearButton setHidden:YES];
    _clearButton.frame = CGRectMake(SCREEN_WIDTH - 23 - 15, 10, 15, 15);
    [_clearButton setBackgroundImage:[UIImage imageNamed:@"清除"] forState:UIControlStateNormal];
    [_clearButton addTarget:self action:@selector(clearButtonClicker) forControlEvents:UIControlEventTouchUpInside];
    [_adviceTV addSubview:_clearButton];
    
    self.navigationItem.rightBarButtonItem = [[UIBarButtonItem alloc] initWithTitle:@"提交" style:UIBarButtonItemStylePlain target:self action:@selector(sureBtnClicked)];
}

- (void)sureBtnClicked {
    if ([self.adviceTV.text isEqualToString:@""]) {
        [HMAlertView bottomAlertView:@"没有反馈内容"];
        return;
    }
    
    AVUser *currentUser = [AVUser currentUser];
    NSString *userId = currentUser[@"objectId"];
    
    AVObject *userIconObj = [[AVObject alloc] initWithClassName:@"Feedback"];
    
    [userIconObj setObject:userId forKey:@"userId"];
    [userIconObj setObject:self.adviceTV.text forKey:@"content"];
    [userIconObj saveEventually];
    [HMAlertView bottomAlertView:@"意见反馈成功"];
    [self.navigationController popViewControllerAnimated:YES];
}

//清除按钮方法
- (void)clearButtonClicker {
    _adviceTV.text = @"";
    [_placeHolderL setHidden:NO];
    [_clearButton setHidden:YES];
}

#pragma mark --表格视图代理方法
- (BOOL)textFieldShouldReturn:(UITextField *)textField {
    [textField resignFirstResponder];
    return YES;
}

#pragma mark --视图将要显示和消失
- (void)viewWillAppear:(BOOL)animated {
    //注册监听键盘弹出回收通知
    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(notificationWithKeyboard:) name:UIKeyboardWillChangeFrameNotification object:nil];

}
//通知回调方法
- (void)notificationWithKeyboard:(NSNotification *)notify {
    CGRect keyBoardRect = [notify.userInfo[@"UIKeyboardFrameEndUserInfoKey"] CGRectValue];
    CGFloat keyBoardY = keyBoardRect.origin.y;
    
    
   
    self.adviceTV.frame = CGRectMake(8, 64, SCREEN_WIDTH - 16, keyBoardY - 64);
}


- (void)viewWillDisappear:(BOOL)animated {
    
}

#pragma mark --textview代理方法
- (void)textViewDidChange:(UITextView *)textView {
    if (![textView  isEqual: @""]) {
        [_placeHolderL setHidden:YES];
        [_clearButton setHidden:NO];
    }else {
        [_placeHolderL setHidden:NO];
        [_clearButton setHidden:YES];
    }
}

@end
